iT邦幫忙

2023 iThome 鐵人賽

DAY 19
0
Software Development

Should I use fp-ts系列 第 19

[Should I use fp-ts?] Day 19 - fp-ts Either usage

  • 分享至 

  • xImage
  •  

在本系列文中,所有的程式碼以及測試都可以在 should-i-use-fp-ts 找到,今日的範例放在 src/day-19 並且有測試可以讓大家練習。

Either usage

今天使用 Either 來改寫 day-17-Option-usage 的範例。

type User = {
  username: string;
  email: string;
  password: string;
};

const moreThan3Chars = (xs: string)  => xs.length > 3 ? xs : throw New Error('Need more than 3 chars');
const validateEmail = (email: string) => email.includes('@') ? email : throw New Error('Invalud email');
const moreThan6Chars = (xs: string) => xs.length > 6 ? xs : throw New Error('Need more than 6 chars');
const hasCapital = (xs: string) => /[A-Z]/.test(xs) ? xs : throw New Error('Need at least one capital letter');
const hasNumber = (xs: string) => /\d/.test(xs) ? xs : throw New Error('Need at least one number');

const validateOriginal = ({ username, email, password }: User) => {
  try {
    moreThan3Chars(username);
    validateEmail(email);
    moreThan6Chars(password);
    hasCapital(password);
    hasNumber(password);
    return { username, email, password };
  } catch (error) {
    return error;
  }
};

這邊使用 trycatch block 來統一所有的 execption 的處理,如果所有條件都和規則回傳使用者,否則拋出 Error,接著使用 Either 來改寫。

const moreThan3Chars = E.fromPredicate(
  (xs: string) => xs.length > 3,
  () => 'Need more than 3 chars',
);
const validateEmail = E.fromPredicate(
  (xs: string) => xs.includes('@'),
  () => 'Invalid email',
);
const moreThan6Chars = E.fromPredicate(
  (xs: string) => xs.length > 6,
  () => 'Need more than 6 chars',
);
const hasCapital = E.fromPredicate(
  (xs: string) => /[A-Z]/.test(xs),
  () => 'Need at least one capital letter',
);
const hasNumber = E.fromPredicate(
  (xs: string) => /\d/.test(xs),
  () => 'Need at least one number',
);

const validate = ({ username, email, password }: User) => pipe(
  username,
  moreThan3Chars,
  E.bindTo('username'),
  E.apS('email', validateEmail(email)),
  E.apS('password', pipe(
    password,
    moreThan6Chars,
    E.chain(hasCapital),
    E.chain(hasNumber),
  )),
);

今天的主題在 should-i-use-fp-tssrc/day-19 並且有測試可以讓大家練習。

Reference

Either.ts | fp-ts


上一篇
[Should I use fp-ts?] Day 18 - fp-ts Either type and constructor
下一篇
[Should I use fp-ts?] Day 20 - fp-ts Either trycatch
系列文
Should I use fp-ts25
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言